package com.cjkj.system.controller;

import com.cjkj.common.security.annotation.RepeatSubmit;
import com.cjkj.common.core.utils.IDCardValidator;
import com.cjkj.common.core.web.controller.BaseController;
import com.cjkj.common.core.web.domain.AjaxResult;
import com.cjkj.common.core.web.page.TableDataInfo;
import com.cjkj.common.log.annotation.Log;
import com.cjkj.common.log.enums.BusinessType;
import com.cjkj.common.security.utils.SecurityUtils;
import com.cjkj.system.api.domain.THospitalUser;
import com.cjkj.system.domain.dto.DiagnosisReq;
import com.cjkj.system.domain.dto.PatVisitReq;
import com.cjkj.system.domain.dto.PatientEditInfoReq;
import com.cjkj.system.domain.dto.VisitRecordReq;
import com.cjkj.system.domain.vo.PatVisitListVo;
import com.cjkj.system.domain.vo.PatVisitSimpleVo;
import com.cjkj.system.service.ITPatIndexService;
import com.cjkj.system.service.ITPatVisitService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/patient")
public class TPatVisitController extends BaseController {

    @Resource
    private ITPatVisitService patVisitService;

    @Resource
    private ITPatIndexService patIndexService;

    /**
     * 患者列表查询
     * @param req
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(PatVisitReq req){
        THospitalUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        startPage();
        req.setHospitalId(sysUser.getHospitalId());
        req.setDeptId(sysUser.getDeptId());
        req.setDoctorId(sysUser.getUserId());
        List<PatVisitListVo> list = patVisitService.getTPatVisitList(req);
        return getDataTable(list);
    }

    /**
     * 新增患者
     * @param req
     * @return
     */
    @PostMapping
    @RepeatSubmit
    @Log(title = "患者登记",businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody @Valid PatientEditInfoReq req){

        req.setIdentityNumber(req.getIdentityNumber().toUpperCase());
        if(! IDCardValidator.isValid(req.getIdentityNumber())) {
            return AjaxResult.error("身份证号码不合法！");
        }

        patVisitService.add(req);
        return AjaxResult.success("患者登记成功！");
    }

    /**
     * 查询患者信息
     * 用于修改回显数据
     * @param visitNo
     * @return
     */
    @GetMapping("/{visitNo}")
    public AjaxResult get(@PathVariable("visitNo") Long visitNo){
        return AjaxResult.success(patVisitService.getInfo(visitNo));
    }

    /**
     * 修改患者信息
     * @param req
     * @return
     */
    @PutMapping()
    @RepeatSubmit
    @Log(title = "患者修改",businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody @Valid PatientEditInfoReq req){
        req.setIdentityNumber(req.getIdentityNumber().toUpperCase());
        if(! IDCardValidator.isValid(req.getIdentityNumber())) {
            return AjaxResult.error("身份证号码不合法！");
        }

        patVisitService.update(req);
        return AjaxResult.success("患者信息修改成功！");
    }

    /**
     * 患者出院
     */
    @PutMapping("/leaveHospital")
    @RepeatSubmit
    @Log(title = "患者出院",businessType = BusinessType.UPDATE)
    public AjaxResult leaveHospital(Long visitNo){
        patVisitService.leaveHospital(visitNo);
        return AjaxResult.success("出院成功");
    }
    /**
     * 患者批量出院
     */
    @PutMapping("/batchLeaveHospital")
    @RepeatSubmit
    @Log(title = "患者批量出院",businessType = BusinessType.UPDATE)
    public AjaxResult batchLeaveHospital(String visitNos){
        patVisitService.batchLeaveHospital(visitNos);
        return AjaxResult.success("出院成功");
    }

    /**
     * 删除患者信息
     * @param visitNo
     * @return
     */
    @DeleteMapping("/{visitNo}")
    @Log(title = "删除患者",businessType = BusinessType.DELETE)
    public AjaxResult delete(@PathVariable("visitNo") Long visitNo){
        return AjaxResult.success(patVisitService.deleteTPatVisitByVisitNo(visitNo));
    }

    @GetMapping("/baseInfo")
    public AjaxResult getUserBaseInfo(String identityNumber){
        String hospitalId = SecurityUtils.getLoginUser().getSysUser().getHospitalId();
        return AjaxResult.success(patIndexService.getByIdentityNumber(identityNumber, hospitalId));
    }

    /**
     * 获取患者设备使用情况
     * 用于判断未正确使用体温传感器的患者
     * 和判断未正确佩戴设备的患者
     * @return
     */
    @GetMapping("getUsage")
    public AjaxResult getUsage(){
        return AjaxResult.success(patVisitService.getUsage());
    }

    /**
     * 医院账户查询患者列表
     * @param req
     * @return
     */
    @GetMapping("/allList")
    public TableDataInfo allList(PatVisitReq req){
        startPage();
        req.setHospitalId(SecurityUtils.getLoginUser().getSysUser().getHospitalId());
        List<PatVisitListVo> list = patVisitService.getAllList(req);
        return getDataTable(list);
    }

    @GetMapping("/list/patVisit")
    public TableDataInfo patVisit(PatVisitReq req){
        THospitalUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        startPage();
        req.setHospitalId(sysUser.getHospitalId());
        req.setDeptId(sysUser.getDeptId());
        req.setDoctorId(sysUser.getUserId());
        List<PatVisitSimpleVo> list = patVisitService.getVisitList(req);
        return getDataTable(list);
    }

    /**
     * 查询患者信息
     * 用于监测报告中数据回显
     * @param visitNo
     * @return
     */
    @GetMapping("/info/{visitNo}")
    public AjaxResult info(@PathVariable("visitNo") Long visitNo){
        return AjaxResult.success(patVisitService.getDetailInfo(visitNo));
    }

    /**
     * 诊断
     * @param req
     * @return
     */
    @PostMapping("/doDiagnosis")
    public AjaxResult doDiagnosis(@Valid @RequestBody DiagnosisReq req){
        return AjaxResult.success(patVisitService.doDiagnosis(req));
    }

    /**
     * 获取患者就诊记录
     * @param req
     * @return
     */
    @GetMapping("getVisitRecords")
    public AjaxResult getVisitRecords(@Valid VisitRecordReq req){
        return AjaxResult.success(patVisitService.getVisitRecords(req));
    }

    /**
     * 患者一键续绑
     * @return
     */
    @PutMapping("continueBind")
    @RepeatSubmit
    @Log(title = "患者续绑",businessType = BusinessType.INSERT)
    public AjaxResult continueBind(Long visitNo){
        return AjaxResult.success(patVisitService.continueBind(visitNo));
    }
}
